/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
package com.airlogger.log.model
{
	import com.bourre.collection.Iterator;
	import com.bourre.collection.Stack;
	import com.bourre.log.LogLevel;
	import com.bourre.model.AbstractModel;
	import com.bourre.plugin.Plugin;

	public class LogModel extends AbstractModel
	{
		static public const EQUALS : Number = 0;
		static public const GREATER_THAN : Number = 1;
				
		protected var _cLogStack : Stack;
		protected var _oLevelFilter : LogLevel;
		
		protected var _nFilterMode : Number;
		
		public function LogModel( owner : Plugin = null, name : String = null )
		{
			super( owner, name );
			
			_cLogStack = new Stack( LogData );
			_oLevelFilter = LogLevel.DEBUG;
			_nFilterMode = GREATER_THAN;
		}
		
		public function addLog ( log : LogData ):void
		{
			_cLogStack.push( log );
		}
		public function clearLogs ():void
		{
			_cLogStack = new Stack( LogData );
		}
		
		public function setLogFilter ( level : LogLevel ) : void
		{
			_oLevelFilter = level;	
		}
		public function getLogFilter () : LogLevel
		{
			return _oLevelFilter;
		}
		
		public function getFilteredLogs () : Stack
		{
			var s : Stack = new Stack( LogData );
			var i : Iterator = _cLogStack.iterator();

			while( i.hasNext() )
			{
				var data : LogData = i.next() as LogData;
				
				if( _nFilterMode == EQUALS )
				{
					if( data.level.getLevel() == _oLevelFilter.getLevel() )
					{
						s.add( data );
					}
				}
				else
				{				
					if( data.level.getLevel() >= _oLevelFilter.getLevel() )
					{
						s.add( data );
					}
				}
			}
			
			return s;
		}
		
		public function setFilterMode(mode : Number) : void
		{
			_nFilterMode = mode;
		}
		
		public function getFilterMode () : Number
		{
			return _nFilterMode;
		}
	}
}